//
//  LRNetwork+Helper.m
//  LRShop
//
//  Created by HCW on 16/8/4.
//  Copyright © 2016年 HCW. All rights reserved.
//

#import "LRNetwork+Helper.h"

@implementation LRNetwork (Helper)

+ (NSDictionary *)requestParameter:(NSDictionary *)parameter
{
    NSMutableDictionary *dict = parameter.mutableCopy;
    
    [dict setValue:allConstProjectid?:@"" forKey:@"project_id"];
    [dict setValue:allConstMD5id?:@"" forKey:@"sign"];
    [dict setValue:allConstMemberid?:@"" forKey:@"user_id"];
    [dict setValue:allConstApp_id?:@"" forKey:@"app_id"];
    return dict;
}

+ (NSString *)requestURL:(NSString *)methodName
{
    if (![methodName isKindOfClass:[NSString class]] || methodName==nil) {
        return nil;
    }
    
    NSURL *baseURL = [self baseURL];
    
    return [[NSURL URLWithString:methodName relativeToURL:baseURL] absoluteString];
}

+ (NSString *)requestImageURL:(NSString *)url
{
    if (!url) {
        return @"";
    }
    NSURL *baseURL = [self baseURL];
    url = [[NSURL URLWithString:url relativeToURL:baseURL] absoluteString];
    
    return url;
}

#pragma mark - Private Method

+ (NSString *)dictionaryToJson:(NSDictionary *)dic
{
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

+ (NSURL *)baseURL
{
    /* 正式环境 */
    NSURL *baseURL = [NSURL URLWithString:@"http://www.zhejia100.com/"];
    
    /* 电脑环境 */

//    NSURL *baseURL = [NSURL URLWithString:@"http://192.168.31.11:8080/"];
    
    return baseURL;
}
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    
    if (jsonString == nil) {
        
        return nil;
        
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSError *err;
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                         
                                                        options:NSJSONReadingMutableContainers
                         
                                                          error:&err];
    
    if(err) {
        
        NSLog(@"json解析失败：%@",err);
        
        return nil;
        
    }
    
    return dic;
    
}
@end
